MyBatis 您所在的位置:网站首页 mybatis plus 使用page MyBatis

MyBatis

#MyBatis| 来源: 网络整理| 查看: 265

MyBatis-Plus分页插件的配置与使用 1. 分页插件的配置1.1 MyBatis-Plus依赖配置1.2 MyBatis-Plus分页插件配置 2. 分页插件的使用2.1 理论分析2.1 代码实现

1. 分页插件的配置 1.1 MyBatis-Plus依赖配置

注意事项:MyBatis-Plus(以下简称 “MP” )的分页插件是在MP3.4.0版本开始引入的,因此pom文件中必须引入3.4.0版本之后的MP依赖。

com.baomidou mybatis-plus-boot-starter 3.4.3 1.2 MyBatis-Plus分页插件配置

新建配置类MybatisPlusConfig,将MP的分页插件进行配置并注入IOC容器。

@Configuration @MapperScan("com.zhixing.blog.mapper") public class MybatisPlusConfig { //配置分页插件注入容器 @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } } 2. 分页插件的使用 2.1 理论分析

引入MP分页插件之后,程序可以使用Page对象,其中两个参数为current和size,分别代表当前页数和页内记录数量,通过这两个参数,MySQL数据库可以推算出其SQL语句,主要利用limit关键字来进行分页操作。

SQL中 limit 关键字的用法:limit index,size 其中:index代表起始索引值(从0开始),size代表长度 例如:limit 4,5 代表选择索引位置为4至9(4+5)的数据 推算公式:index=(current-1)*size

2.1 代码实现 @SpringBootTest class BlogApplicationTests { @Autowired private IBlogService blogService; @Test void contextLoads() { //查看第一页,每页5条记录 Page page = new Page(1, 5); Page blogPage = blogService.page(page); //获取当前页的所有记录信息列表 List blogs = blogPage.getRecords(); //获取当前页数 System.out.println(blogPage.getCurrent()); //获取总页数 System.out.println(blogPage.getPages()); //获取每页记录数 System.out.println(blogPage.getSize()); } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有